<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs v-model="activeName">
          <el-tab-pane label="登录账户设置" name="first">
            <el-form ref="form" :model="detailInfo" style="margin-left: 120px; margin-top:30px" :rules="rules">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="detailInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码" prop="password2">
                <el-input v-model="detailInfo.password2" type="password" style="width:300px" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit">更新</el-button>
                <el-button @click="$router.push('/employees')">返回</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情" name="second">
            <component :is="userInfoComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="third">
            <component :is="jobInfoComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDetailById, saveDetailById } from '@/api/employees'
import userInfoComponent from './components/user-info.vue'
import jobInfoComponent from './components/job-info.vue'
export default {
  components: { userInfoComponent, jobInfoComponent },
  props: {
    detailId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      userInfoComponent,
      jobInfoComponent,
      loading: false,
      activeName: 'first',
      detailInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password2: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getDetail()
  },
  methods: {
    // 提交修改
    async onSubmit() {
      try {
        await this.$refs.form.validate()
        await saveDetailById({ ...this.detailInfo, password: this.detailInfo.password2 })
        this.$message.success('修改成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 获取员工基本信息
    async getDetail() {
      this.loading = true
      this.detailInfo = await getDetailById(this.detailId)
      this.loading = false
    }
  }
}
</script>

<style>
</style>
